package com.gexin.model.activity;

import com.gexin.common.model.ApiResponse;
import com.gexin.common.utils.SQLUtils;
import com.gexin.common.utils.StringUtil;
import com.gexin.model.activity.base.BaseActivity;
import com.jfinal.plugin.activerecord.Page;
import java.util.List;

public class Activity
        extends BaseActivity<Activity>
{
    public static final Activity dao = new Activity();
    public static Integer YOUHUIJUAN = Integer.valueOf(1);
    public static Integer ZENGSONG = Integer.valueOf(2);
    public static Integer ZHEKOU = Integer.valueOf(3);

    public Page<Activity> findList(int getpage, int getpageSize)
    {
        SQLUtils selSql = new SQLUtils("select ac.*,pro.product_name as productName ");
        SQLUtils sql = new SQLUtils();
        sql.append(" from t_activity ac left join t_products pro on ac.product_id=pro.id ");
        return paginate(getpage, getpageSize, selSql.toString(), sql.toString());
    }

    public ApiResponse deleteActivityById(String id)
    {
        if (StringUtil.isBlank(id)) {
            return new ApiResponse(false, "id������null!");
        }
        List<UserActivity> list = UserActivity.dao.findListByCoupon(id);
        if ((list != null) && (list.size() > 0)) {
            return new ApiResponse(false, "��������������������!��������!");
        }
        boolean delete = dao.deleteById(id);
        return new ApiResponse(delete, delete ? "��������!" : "��������!");
    }

    public List<Activity> findIndexActivity()
    {
        return dao.find("SELECT * from t_activity where NOW()>activity_start_time AND NOW()<activity_end_time");
    }

    public List<Activity> findType(Integer type)
    {
        return dao.find("SELECT * from t_activity where NOW()>activity_start_time AND NOW()<activity_end_time and type=? ", new Object[] { type });
    }

    public Activity findByProductSkuId(String skuId)
    {
        return (Activity)dao.findFirst("SELECT * from t_activity where NOW()>activity_start_time AND NOW()<activity_end_time and product_sku_id=?", new Object[] { skuId });
    }

    public Activity findByProductId(Integer productId)
    {
        return (Activity)dao.findFirst("SELECT * from t_activity where NOW()>activity_start_time AND NOW()<activity_end_time and product_id=?", new Object[] { productId });
    }
}
